﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using GKFX.Common.UI.MVC;
using Century.Model;
using GKFX.Business.Repository;
using Telerik.Web.Mvc;
using GKFX.Common.Business.BOL.Common;
using System.Web.Services;

namespace Century.Web.MVC.AdminArea
{
    public class CountrySiteScriptController : GKFXAdminBaseController
    {
        CountrySiteScriptBSO cssDB = new CountrySiteScriptBSO();

        public ActionResult Index()
        {
            return View();
        }

        public ActionResult PageScript(int? id)
        {
            ViewBag.AccessibleSites = Token.AccessibleSites;
            ViewBag.RenderPosition = Enum.GetValues(typeof(GKFX.Common.Enumerations.CountrySiteScriptPositions)).Cast<GKFX.Common.Enumerations.CountrySiteScriptPositions>().ToList().Select( v => new {Id = (int)v, Name=v.ToString()});
            SitePage page = new SitePage();
            if (id.HasValue)
            {
                Session["CurrentPageId"] = id.Value;
                page = PersistData.AvailablePages.Find(p => p.Id == id.Value);
            }

            return View(page);
        }
        
        [GridAction]
        public ActionResult GetPageScripts(int? id)
        {
            var d = new GridModel() { Data = new List<CountrySiteScript>() };
            string pageName = string.Empty; // if name is null , gets all scripts for site

            if (id.HasValue)
            {
                SitePage page = PersistData.AvailablePages.Find(p => p.Id == id.Value);
                pageName = string.IsNullOrEmpty(page.LocalName) ? page.Name : page.LocalName;
            }

            //Rebind the grid
            d = new GridModel
            {
                Data = cssDB.GetCountrySiteScripts(PersistData.CurrentSite.Id, pageName, null).Select(o => new
                {
                    o.Id,
                    o.IsActive,
                    o.PageName,
                    o.RenderPosition,
                    o.Script,
                    o.Description,
                    o.ScriptKey,
                    o.CountrySiteId,
                    o.ExecutionOrder
                })
            };

            return View(d);
        }

        [AcceptVerbs(HttpVerbs.Post)]
        [GridAction]
        public ActionResult InsertPageScripts(int? id)
        {
            //Create a new instance of the EditableProduct class.
            CountrySiteScript script = new CountrySiteScript();

            SitePage page = new SitePage();
            if (id.HasValue)
            {
                page = PersistData.AvailablePages.Find(p => p.Id == id.Value);
            }

            //Perform model binding (fill the product properties and validate it).
            TryUpdateModel(script);
            script.Id = 0;
            script.ScriptKey = Guid.NewGuid().ToString();
            script.PageName = id.HasValue ? (string.IsNullOrEmpty(page.LocalName) ? page.Name : page.LocalName) : null;
            script.ExecutionOrder = 0;
            script.CountrySiteId = PersistData.CurrentSite.Id;
            script.CreateDate = DateTime.Now;
            //The model is valid - insert the script.
            cssDB.Insert(script);

            //Rebind the grid
            return _commonCRUDResult();
        }

        [AcceptVerbs(HttpVerbs.Post)]
        [GridAction]
        public ActionResult UpdatePageScripts(int id)
        {
            CountrySiteScript script = cssDB.Find(p => p.Id == id);
            TryUpdateModel(script);
            cssDB.Update(script);

            //Rebind the grid
            return _commonCRUDResult();
        }

        private ActionResult _commonCRUDResult()
        {
            if (ControllerContext.RouteData.Values["id"] != null)
            {
                return GetPageScripts(int.Parse(ControllerContext.RouteData.Values["id"].ToString()));
            }
            else
            {
                var d = new GridModel
                {
                    Data = cssDB.GetAll().Select(o => new
                    {
                        o.Id,
                        o.IsActive,
                        o.PageName,
                        o.RenderPosition,
                        o.Script,
                        o.Description,
                        o.ScriptKey,
                        o.CountrySiteId,
                        o.ExecutionOrder
                    }).OrderBy(c => c.Id)
                };
                return View(d);
            }
        }


        #region Remote Checkers

        public JsonResult ScriptUniqueValidation(string pageName, string scriptKey)
        {
            if (!string.IsNullOrEmpty(pageName) || !string.IsNullOrEmpty(scriptKey))
            {
                return Json(true, JsonRequestBehavior.AllowGet);
            }
            else
            {
                return Json("Page Name or Script Key must be filled.", JsonRequestBehavior.AllowGet);
            }
        }

        #endregion
    }
}
